package cas;

import sun.misc.Unsafe;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * cas使用demo
 *
 * @author <a href="mailto:huangzf3@asiainfo.com">huangzf</a>
 * @version 1.0.0
 * @since 2022/08/14 上午 09:49
 */
public class CASdemo {

    final static AtomicInteger x = new AtomicInteger(0);

    public static void main(String[] args) {
        //初始化设置值为5
        x.set(5);
        //预期值和当前值都是5，更新成功，x值为2022
        System.out.println(x.compareAndSet(5,2022)+"\tx当前值为"+x.get());
        //预期值为5，当前值为2022；更新失败，x的值不会被修改
        System.out.println(x.compareAndSet(5,2021)+"\tx当前值为"+x.get());
       x.incrementAndGet();

    }
}
